package com.osmino.contacts.history;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.osmino.contacts.R;
import com.osmino.contacts.entity.Call;
import com.osmino.core.utils.view.Backgroung;

public final class CallLogCell extends RelativeLayout {

	private final TextView mWhoText, mTypeText, mDayText, mTimeText, mDurationText;
	private final ImageView mOutgoingIcon;
	private static final int BG_ONE = R.drawable.base_list_bg_1;
	private static final int BG_TWO = R.drawable.base_list_bg_2;
	private static final int MISSED_CALL_COLOR = Color.rgb(218,39,98);
	private static final int UNMISSED_CALL_COLOR = Color.rgb(28, 28, 35);
	private static final int PHONETYPE_N_TIME_COLOR = Color.rgb(142, 142, 142);

	public CallLogCell(Context context) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.history_call_item, this);
		mWhoText = (TextView) findViewById(R.id.history_call_who);
		mTypeText = (TextView) findViewById(R.id.history_call_telephone_type);
		mDayText = (TextView) findViewById(R.id.history_call_day);
		mTimeText = (TextView) findViewById(R.id.history_call_time);
		mDurationText = (TextView) findViewById(R.id.history_call_duration);
		mOutgoingIcon = (ImageView) findViewById(R.id.history_call_type);
	}

	public void setCallInfo(Call info) {
		String who = info.cachedName;
		if (who == null || who.equals("")) {
			who = info.number;
		}
		mWhoText.setText(who);
		mTypeText.setText(info.cachedNumberLabel);
		mDayText.setText(info.getDate(getContext()));
		mTimeText.setText(info.getTime());
		mDurationText.setText(String.valueOf(info.getDuration()));
		if (info.isMissed()) {
			setCallAsMissed();
		} else if (info.isIncoming()) {
			setCallAsIncoming();
		} else if (info.isOutgoing()) {
			setCallAsOutgoing();
		}
	}
	
	private void setCallAsMissed() {
		mDurationText.setVisibility(GONE);
		mOutgoingIcon.setVisibility(GONE);
		setColorForTextViews(MISSED_CALL_COLOR);
	}
	
	private void setCallAsUnmissed() {
		mDurationText.setVisibility(VISIBLE);
		setColorForTextViews(UNMISSED_CALL_COLOR);
	}
	
	private void setCallAsIncoming() {
		setCallAsUnmissed();
		mOutgoingIcon.setVisibility(GONE);
	}
	
	private void setCallAsOutgoing() {
		setCallAsUnmissed();
		mOutgoingIcon.setVisibility(VISIBLE);
	}
	
	private void setColorForTextViews(int color) {
		mWhoText.setTextColor(color);
		mDayText.setTextColor(color);
	}

	public void setBackground(boolean isOdd) {
		int colorWhite = Color.rgb(255, 255, 255);
		int colorLightGray = Color.rgb(247, 247, 247);
		int colorAct = Color.rgb(200, 200, 200);
		if(isOdd){
			Backgroung.setBackgroundColor(this, colorWhite, colorAct);
		}
		else{
			Backgroung.setBackgroundColor(this, colorLightGray, colorAct);
		}
	}
}
